package game;

import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import javax.microedition.media.control.GUIControl;
import javax.microedition.media.control.VideoControl;

/**
 * This class handles all things which have to be done for a pretty nice intro!
 * @author Gustav
 *
 */

public class Intro implements CommandListener, PlayerListener, Runnable{
    private javax.microedition.media.Player p;

    /**
     * Start a new Thread for the player
     */
    public void start(){
        Thread t = new Thread(this);
        t.start();
    }

    public void playerUpdate(Player player, String event, Object eventData) {
        System.out.println(event);
        if(event.compareTo(PlayerListener.END_OF_MEDIA) == 0 || event.compareTo(PlayerListener.STOPPED)==0){
            if (player != null) {
                if(player.getState() == Player.STARTED){
                    try {
                        player.stop();
                    } catch (MediaException ex) {
                        ex.printStackTrace();
                    }
                }
                if(player.getState() == Player.PREFETCHED)
                    player.deallocate();
                if(player.getState() == Player.REALIZED || player.getState() == Player.UNREALIZED)
                    player.close();
            }
            player = null;
            LUMM2_Game.currentGameState = LUMM2_Game.MENU;
            Display.getDisplay(LUMM2_Canvas.midlet).setCurrent(LUMM2_Midlet.pc);
        }
    }

    public void run() {
    	Form mForm = new Form("video");
        InputStream videoStream = getClass().getResourceAsStream("/intro.mpg");
        try {
            p = Manager.createPlayer(videoStream, "video/mpeg");
            p.addPlayerListener(this);
            p.realize();

            VideoControl vc = (VideoControl)p.getControl("VideoControl");
            if (vc != null) {
            	Display.getDisplay(LUMM2_Canvas.midlet).setCurrent(mForm);
                mForm.addCommand(new Command("Stop", Command.STOP, 1));
                mForm.setCommandListener(this);
            	mForm.append((Item)vc.initDisplayMode(GUIControl.USE_GUI_PRIMITIVE, null));
            	vc.setDisplayFullScreen(true);
            	vc.setVisible(true);
            }

            p.prefetch();
            p.start();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (MediaException ex) {
            ex.printStackTrace();
        }
    }

    public void commandAction(Command c, Displayable d) {
        try {
            p.stop();
        } catch (MediaException ex) {
            ex.printStackTrace();
        }
    }
}
